package com.guguskins.back.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guguskins.back.common.constant.PrizeType;
import com.guguskins.back.common.exception.GlobalMsgException;
import com.guguskins.back.entity.GuBox;
import com.guguskins.back.entity.GuGoods;
import com.guguskins.back.entity.GuInviteNum;
import com.guguskins.back.entity.GuProp;
import com.guguskins.back.mapper.GuInviteNumMapper;
import com.guguskins.back.service.GuBoxService;
import com.guguskins.back.service.GuGoodsService;
import com.guguskins.back.service.GuInviteNumService;
import com.guguskins.back.service.GuPropService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-06-02
 */
@Service
public class GuInviteNumServiceImpl extends ServiceImpl<GuInviteNumMapper, GuInviteNum> implements GuInviteNumService {

    @Autowired
    private GuInviteNumMapper guInviteNumMapper;

    @Autowired
    private GuGoodsService guGoodsService;

    @Autowired
    private GuBoxService boxService;

    @Autowired
    private GuPropService propService;

    @Override
    public List<GuInviteNum> backList() {
        List<GuInviteNum> inviteNums = this.list();
        for (GuInviteNum inviteNum : inviteNums) {
            switch (inviteNum.getInvitePrizeType()) {
                case PrizeType.BOX:
                    GuBox box = boxService.getBox(inviteNum.getInvitePrizeId());
                    inviteNum.setItemName(box.getBoxName());
                    inviteNum.setPrizeImage(box.getBoxImageUrl());
                    inviteNum.setPrizeGoodsImage(box.getGoodsList().stream().max(Comparator.comparing(GuGoods::getGoodsPrice)).orElse(new GuGoods()).getGoodsIconUrl());
                    break;
                case PrizeType.GOODS:

             GuGoods guGoods=guGoodsService.getById(inviteNum.getInvitePrizeId());
                    inviteNum.setItemName(guGoods.getGoodsName());
             inviteNum.setPrizeImage(guGoods.getGoodsIconUrl());
                    break;
                case PrizeType.PROP:
                    GuProp prop = propService.getById(inviteNum.getInvitePrizeId());
                    inviteNum.setItemName(prop.getPropName());
                    inviteNum.setPrizeImage(prop.getPropImage());
                    break;
                default:
                    throw new GlobalMsgException("系统错误");
            }
        }
        return inviteNums;
    }

    @Override
    public List<GuInviteNum> findAllList() {

        List<GuInviteNum> inviteNums = guInviteNumMapper.selectList(null);

        inviteNums.forEach(inviteNum -> {
            if (inviteNum.getInvitePrizeType() == 1) {
                inviteNum.setGuBox(boxService.getById(inviteNum.getInvitePrizeId()));
            } else {
                inviteNum.setGuGoods(guGoodsService.getById(inviteNum.getInvitePrizeId()));
            }
        });

        return inviteNums;
    }
}
